﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class PositionDutyDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PositionDutyDAL()
        {

        }
        public PositionDutyInfo GetPositionDutyInfo(Nullable<int> seq)
        {
            string strSQL;
            PositionDutyInfo Info = new PositionDutyInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@Seq", seq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT pd.POS_DUTY_SEQ, pd.POS_SEQ, pd.DUTY_SEQ, pd.REMARKS"
                        + ", f.FIELD_POS_NAME, f.POS_TYPE_SEQ, pt.POS_TYPE_NAME, d.DUTY_NAME"
                    + " FROM HRM_MP_POSITION_DUTIES pd"
                        + " LEFT JOIN HRM_MP_DUTIES d ON pd.DUTY_SEQ = d.DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON pd.POS_SEQ = p.POS_SEQ"
                        + " LEFT JOIN HRM_MP_FIELD_POSITIONS f ON p.FIELD_POS_SEQ = f.FIELD_POS_SEQ"
                        + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON f.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                    + " WHERE pd.RECORD_STATUS not like @DeleteRecord"
                        + " AND pd.POS_DUTY_SEQ = @Seq";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    Info.Seq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                    Info.PositionSeq = rdr.IsDBNull(1) ? (int?)null : rdr.GetInt32(1);
                    Info.DutySeq = rdr.IsDBNull(2) ? (int?)null : rdr.GetInt32(2);
                    Info.Remarks = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
                }

                return Info;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {

                dbManager.Dispose();
            }
        }
        public IList<PositionDutyInfo> LoadGridPositionDuties(Nullable<int> EmpTypeSeq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<PositionDutyInfo> infoList = new List<PositionDutyInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@EmpTypeSeq", EmpTypeSeq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT pd.POS_DUTY_SEQ, pd.POS_SEQ, pd.DUTY_SEQ, pd.REMARKS"
                        + ", p.POS_REF, p.POS_NAME, d.DUTY_NAME, p.FIELD_POS_SEQ"
                        + ", f.FIELD_POS_NAME, f.POS_TYPE_SEQ, pt.POS_TYPE_NAME"
                    + " FROM HRM_MP_POSITION_DUTIES pd"
                        + " LEFT JOIN HRM_MP_DUTIES d ON pd.DUTY_SEQ = d.DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON pd.POS_SEQ = p.POS_SEQ"
                        + " LEFT JOIN HRM_MP_FIELD_POSITIONS f ON p.FIELD_POS_SEQ = f.FIELD_POS_SEQ"
                        + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON f.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                    + " WHERE pd.RECORD_STATUS not like @DeleteRecord"
                        + " AND p.EMP_TYPE_SEQ = @EmpTypeSeq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        PositionDutyInfo info = new PositionDutyInfo();
                        info.Seq = Convert.IsDBNull(dr[i]["POS_DUTY_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_DUTY_SEQ"].ToString());
                        info.PositionSeq = Convert.IsDBNull(dr[i]["POS_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_SEQ"].ToString());
                        info.DutySeq = Convert.IsDBNull(dr[i]["DUTY_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["DUTY_SEQ"].ToString());
                        info.PositionTypeSeq = Convert.IsDBNull(dr[i]["POS_TYPE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_TYPE_SEQ"].ToString());
                        info.FieldSeq = Convert.IsDBNull(dr[i]["FIELD_POS_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["FIELD_POS_SEQ"].ToString());
                        info.PositionRefNo = Convert.IsDBNull(dr[i]["POS_REF"]) ? "" : (dr[i]["POS_REF"]).ToString();
                        info.PositionName = Convert.IsDBNull(dr[i]["POS_NAME"]) ? "" : (dr[i]["POS_NAME"]).ToString();
                        info.DutyName = Convert.IsDBNull(dr[i]["DUTY_NAME"]) ? "" : (dr[i]["DUTY_NAME"]).ToString();
                        info.PositionTypeName = Convert.IsDBNull(dr[i]["POS_TYPE_NAME"]) ? "" : (dr[i]["POS_TYPE_NAME"]).ToString();
                        info.FieldName = Convert.IsDBNull(dr[i]["FIELD_POS_NAME"]) ? "" : (dr[i]["FIELD_POS_NAME"]).ToString();
                        info.Remarks = Convert.IsDBNull(dr[i]["REMARKS"]) ? "" : (dr[i]["REMARKS"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }
        
        public void Insert(PositionDutyInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@PosSeq", Info.PositionSeq);
                dbManager.AddParameters(1, "@DutySeq", Info.DutySeq);
                dbManager.AddParameters(2, "@Remarks", Info.Remarks);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_POSITION_DUTIES(POS_SEQ, DUTY_SEQ"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@PosSeq, @DutySeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(PositionDutyInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@PosDutySeq", Info.Seq);
                dbManager.AddParameters(1, "@PosSeq", Info.PositionSeq);
                dbManager.AddParameters(2, "@DutySeq", Info.DutySeq);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_POSITION_DUTIES"
                           + " SET POS_SEQ = @PosSeq"
                                + ", DUTY_SEQ = @DutySeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE POS_DUTY_SEQ = @PosDutySeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(PositionDutyInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Info.Seq);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_POSITION_DUTIES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE POS_DUTY_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
